package com.gpstracking.Servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.gpstracking.BE.Usuario;
import com.gpstracking.BL.UsuarioBL;

/**
 * Servlet implementation class ServletCreateArea
 */
public class ServletLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public ServletLogin() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		try{
			
			
			UsuarioBL usuarioBL = new UsuarioBL();
			HttpSession sesion = request.getSession(true);
			Usuario usuario = null;
			if ( (usuario = usuarioBL.validar( request.getParameter("usuario"), request.getParameter("clave"))) != null){
				sesion.setAttribute("user", usuario);
				
				String url = (String)sesion.getAttribute("url");
				if ( url == null )
					response.sendRedirect("../index.jsp");
				else 
					response.sendRedirect(url);
			}else{
				response.sendRedirect("../login.jsp");
				
			}
		}catch( Exception ex){
			ex.printStackTrace();
		}
	}

}
